{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "211661e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "29"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数字\n",
    "import random\n",
    "# 导入random模块\n",
    "# 调用random.randint函数  生成随机数\n",
    "wait_time = random.randint(1,60)\n",
    "wait_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "01254ca8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bottles'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串\n",
    "word = \"bottles\"\n",
    "word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "72bc6f02",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 按字面创建列表\n",
    "prices = []\n",
    "\n",
    "temps = [32.0,212.0,0.0,81.6]\n",
    "words = [\"hello\",\"world\"]\n",
    "car_details = [\"Toyota\",\"RAV4\",2.2,60807]\n",
    "everyting = [prices,temps,words,car_details]\n",
    "\n",
    "odds_and_ends = [[1,2,3],['a','b','c'],['One','Two','Three']]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1c893cb9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "e\n",
      "i\n",
      "o\n",
      "u\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = \"Milliways\"\n",
    "for letter in vowels:\n",
    "    if letter in vowels:\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e33ffddf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = []\n",
    "len(found)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "359dfb1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ee = ['a']\n",
    "len(ee)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0e223f61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'i', 'o']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在运行时扩展列表\n",
    "ee.append('e')\n",
    "ee.append('i')\n",
    "ee.append('o')\n",
    "len(ee)\n",
    "ee"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6887aa7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'i', 'o', 'u']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用 in 检查成员关系\n",
    "if 'u' not in ee:\n",
    "    ee.append('u')\n",
    "ee"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "793a7b18",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:Milliways\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = []\n",
    "for letter in words:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "        print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "513cdc6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从列表删除对象\n",
    "# remove：取一个对象值作为唯一参数\n",
    "nums = [1,2,3,4]\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "7ce08e20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 4]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums.remove(3)\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "2b6d75ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3]\n"
     ]
    }
   ],
   "source": [
    "# 从列表弹出对象\n",
    "# pop：取一个可选的索引值作为参数\n",
    "sc = [1,2,3,4]\n",
    "sc.pop()  # 没有告诉pop要删除哪个对象，就会自动弹出列表的最后一项\n",
    "sc.pop(0) # 这是索引值，0对应列表第一个对象\n",
    "print(sc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "233df34e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 9, 6]\n"
     ]
    }
   ],
   "source": [
    "# 用对象扩展列表\n",
    "# extend：取一个可选的索引值作为参数\n",
    "zj = [1,2,3]\n",
    "zj.extend([9,6])\n",
    "print(zj)\n",
    "zj.extend(sc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "21daca11",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'two-and-a-half', 3, 4]\n"
     ]
    }
   ],
   "source": [
    "# 在列表中插入一个对象\n",
    "# insert：取一个索引值和一个对象作为参数\n",
    "num = [2,3,4]\n",
    "num.insert(0,1)  # 此处的0是指插入对象的索引在这个位置之前，1则是要插入的值（即对象）\n",
    "num.insert(2,\"two-and-a-half\")\n",
    "print(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ff4f4a1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "# 练习：把字符串“Don't panic!”转换成“on tap”\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3))\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1d9ef2f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [1, 2, 3, 4, 5] \n",
      " stu_id_copydata: [1, 2, 3, 4, 5]\n",
      "stu_id: [1, 3, 4, 5] \n",
      " stu_id_copydata: [1, 3, 4, 5]\n",
      "stu_id: [1, 4, 5] \n",
      " stu_id_matedata: [1, 3, 4, 5]\n",
      "stu_id: [1, 5] \n",
      " stu_id[:]: [1, 5]\n"
     ]
    }
   ],
   "source": [
    "# 复制\n",
    "# 把一个现有列表复制到另一个列表中，你可能想使用赋值操作符：\n",
    "# 1. copy()很重要\n",
    "# 思考，赋值是真正的复制么？ 赋值(=) <=> copy? 不等价\n",
    "stu_id = [1,2,3,4,5]\n",
    "stu_id_copydata = stu_id\n",
    "print(\"stu_id:\",stu_id,\"\\n\",\"stu_id_copydata:\",stu_id_copydata)\n",
    "\n",
    "# 实验1 赋值\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_copydata:',stu_id_copydata)\n",
    "\n",
    "# 实验2 copy()\n",
    "stu_id_matedata = stu_id.copy()\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n",
    "\n",
    "# # 实验3 切片实现复制\n",
    "stu_id_matedata = stu_id[:]\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id[:]:',stu_id_copydata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0b8e7fe4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|\n"
     ]
    }
   ],
   "source": [
    "# 切片\n",
    "# 列表入门\n",
    "# 列表中0代表第一个字符/第一个[]/、1代表第二个，以此类推。（# 列表是有顺序的一组值，顺序是从 0 位置开始的）\n",
    "user_data = \"zhiyu||yuyongkang||zhubi||linlin\"\n",
    "print(user_data[6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "ebdb069f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['linlin', 10, 0]\n",
      "[['zhiyu', 0, 19], ['yuyongkang', 0, 61], ['zhubi', 1, 20]]\n",
      "[['zhiyu', 0, 19], ['yuyongkang', 0, 61]]\n",
      "['zhubi', 1, 20]\n",
      "[['yuyongkang', 0, 61], ['zhubi', 1, 20]]\n",
      "[]\n",
      "[['yuyongkang', 0, 61], ['zhubi', 1, 20], ['linlin', 10, 0]]\n"
     ]
    }
   ],
   "source": [
    "# 引入列表\n",
    "user_data_list = [[\"zhiyu\",0,19],[\"yuyongkang\",00,61],[\"zhubi\",1,20],[\"linlin\",10,0]]\n",
    "# password_list = [0,00,1,10]\n",
    "# age = [19,61,20,0]\n",
    "\n",
    "# 1. 从左往右取值（index & slice）\n",
    "\n",
    "print(user_data_list[3])\n",
    "# * 切片 slice ，右边的值取不到 例如[0:3] 索引为3的值取不到\n",
    "print(user_data_list[0:3])\n",
    "\n",
    "# * 当从0开始取值时，0 可以忽略不写\n",
    "\n",
    "print(user_data_list[:2])\n",
    "\n",
    "# * -3，-2，-1，0，1，2，3，4..\n",
    "# 2. 从右往左取值(index & slice)\n",
    "\n",
    "print(user_data_list[-2])\n",
    "\n",
    "# * 切片 slice ，右边的值取不到 例如[-3:-1] 索引为-1的值取不到\n",
    "\n",
    "print(user_data_list[-3:-1])\n",
    "\n",
    "# * 如果取值为空：说明slice写错了 例如[-3:0]\n",
    "# * 如果要取到最后一个值，不能写0，直接省略不写\n",
    "\n",
    "print(user_data_list[-3:0])  # 这是错误情况！！\n",
    "print(user_data_list[-3:])  # 这是正确情况~~"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "cf00c4f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['zhiyu', 0, 19], ['yuyongkang', 0, 61], ['zhubi', 1, 20], ['linlin', 10, 0]]\n",
      "[['zhubi', 1, 20], ['linlin', 10, 0]]\n",
      "[]\n",
      "[]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "print(user_data_list[:])  # 列出列表全部内容\n",
    "\n",
    "print(user_data_list[2:])\n",
    "print(user_data_list[-1:1]) # 错\n",
    "print(user_data_list[3:1])  # 错\n",
    "print(user_data_list[-1:-3])  # 这种情况为错误（个人理解：左边的必须在左边，右边同）\n",
    "# 步长，list列表slice ：[start:stop:step]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "19d1e7f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[0, 2, 4]\n"
     ]
    }
   ],
   "source": [
    "# 步长\n",
    "# **如果没有指定开始值，则默认为0；\n",
    "# <br>如果没有指定结束指，则取列表允许的最大值；\n",
    "# <br>如果没有指定步长值，则默认步长为1.**\n",
    "\n",
    "num = list(range(10))\n",
    "print(num)\n",
    "print(num[0:6:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "f1f873c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['T',\n",
       " 'h',\n",
       " 'e',\n",
       " ' ',\n",
       " 'H',\n",
       " 'i',\n",
       " 't',\n",
       " 'c',\n",
       " 'h',\n",
       " 'h',\n",
       " 'i',\n",
       " 'k',\n",
       " 'e',\n",
       " 'r',\n",
       " \"'\",\n",
       " 's',\n",
       " ' ',\n",
       " 'G',\n",
       " 'u',\n",
       " 'i',\n",
       " 'd',\n",
       " 'e',\n",
       " ' ',\n",
       " 't',\n",
       " 'o',\n",
       " ' ',\n",
       " 't',\n",
       " 'h',\n",
       " 'e',\n",
       " ' ',\n",
       " 'G',\n",
       " 'a',\n",
       " 'l',\n",
       " 'a',\n",
       " 'x',\n",
       " 'y']"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 开始和结束列表\n",
    "book = \"The Hitchhiker's Guide to the Galaxy\"\n",
    "booklist = list(book)\n",
    "booklist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "85d2dd36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['T', 'h', 'e']"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "booklist[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "550b3270",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[0:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "6726e5f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Galaxy'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[-6:])\n",
    "\n",
    "# 以上两种将所选的范围转换为了一个字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "e4de84f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"yxalaG eht ot ediuG s'rekihhctiH ehT\""
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表中使用步长\n",
    "\n",
    "backwards = booklist[::-1]\n",
    "''.join(backwards)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "f68c5c9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"TeHthie' ud oteGlx\""
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "every_other = booklist[::2]\n",
    "''.join(every_other)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "7de56e36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hitchhiker'"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[4:14])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "2f82d0b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'rekihhctiH'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[13:3:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f1527fb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
